import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  data: {
    tabs:[{
      id:0,
      name:'全部订单',
      isActive:true
    },
    {
      id:1,
      name:'代付款',
      isActive:false
    },
    {
      id:2,
      name:'待发货',
      isActive:false
    },
    {
      id:3,
      name:'退款/退货',
      isActive:false
    }],
    orders:[]
  },
  async onShow(){
    let token = wx.getStorageSync('token');
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index',
      });    
    } 
    let currentPages =  getCurrentPages();
    let {type} = currentPages[currentPages.length-1].options 
    this.getOrders(type)
    this.ChangeItem(type-1)
    //console.log(res);
  },
  //发送订单请求
  async getOrders(type){
    let res = await request({url:'/my/orders/all',data:{type}});
    this.setData({
      orders:res.orders.map(v=>({...v,create_time_cn:(new Date(v.create_time*1000).toLocaleString())}))
    })
  },
  //点击切换tab栏
  itemChange(e){
    const {index} = e.detail;
    this.ChangeItem(index);
    this.getOrders(index+1);
  },
  ChangeItem(index){
    const list = this.data.tabs;
    list.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
    this.setData({
      tabs:list
    })
  }
})